Promise.prototype.finally = function(callback){
  let P = this.constructor;
  return this.then(
    value => {
        P.resolve(callback()).then(() => value)
    },
    reason => {
        P.resolve(callback()).then(() => { throw reason })
    }
  )
}

Promise.sequence = function (promiseArray) {
  let arr = [];
  async function run() {
      for (let p of promiseArray) {
          let val = await p();
          arr.push(val);
      }
      return arr;
  }

  return run();
};

Promise.allSettled = Promise.allSettled || function (iterable) {
  return new Promise((resolve, reject) => {
      function addElementToResult(i, elem) {
          result[i] = elem;
          elementCount++;
          if (elementCount === result.length) {
              resolve(result);
          }
      }

      let index = 0;
      for (const promise of iterable) {
          // Capture the current value of `index`
          const currentIndex = index;
          promise.then(
              (value) => addElementToResult(
                  currentIndex, {
                      status: 'fulfilled',
                      value
                  }),
              (reason) => addElementToResult(
                  currentIndex, {
                      status: 'rejected',
                      reason
                  }));
          index++;
      }
      if (index === 0) {
          resolve([]);
          return;
      }
      let elementCount = 0;
      const result = new Array(index);
  });
};